/**
 * jims
 */
package com.jims.master.modules.physical.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.doctor.entity.OutpTreatRec;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import com.jims.master.modules.doctor.service.ClinicMasterService;
import com.jims.master.modules.doctor.service.OutpTreatRecService;
import com.jims.master.modules.doctor.service.PatMasterIndexService;
import com.jims.master.modules.oper.entity.ExamType;
import com.jims.master.modules.oper.entity.LabType;
import com.jims.master.modules.oper.service.ClinicItemDictService;
import com.jims.master.modules.oper.service.DiseaseManagementService;
import com.jims.master.modules.oper.service.ExamTypeService;
import com.jims.master.modules.oper.service.LabTypeService;
import com.jims.master.modules.physical.entity.TInformation;
import com.jims.master.modules.physical.entity.TResultGroup;
import com.jims.master.modules.physical.entity.TResultGroupList;
import com.jims.master.modules.physical.entity.TResultOffice;
import com.jims.master.modules.physical.service.PhysicalService;
import com.jims.master.modules.physical.service.TInformationService;
import com.jims.master.modules.physical.service.TResultGroupService;
import com.jims.master.modules.physical.service.TResultOfficeService;
import com.jims.master.modules.sys.entity.BaseUnit;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.entity.TItemComobo;
import com.jims.master.modules.sys.entity.TUnitFz;
import com.jims.master.modules.sys.service.BaseUnitService;
import com.jims.master.modules.sys.service.TItemComoboService;
import com.jims.master.modules.sys.service.TUnitFzService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Controller
 * @author zp
 * @version 2017-06-02
 */
@Controller
@RequestMapping(value = "${adminPath}/physical/")
public class PhysicalController extends BaseController {

	@Autowired
	private PatMasterIndexService patMasterIndexService;
	@Autowired
	private PhysicalService physicalService;
	@Autowired
	private ExamTypeService examTypeService;
	@Autowired
	private LabTypeService labTypeService;
	@Autowired
	private OutpTreatRecService outpTreatRecService;
	@Autowired
	private ClinicMasterService clinicMasterService;
	@Autowired
	TInformationService tInformationService;
	@Autowired
	private TItemComoboService tItemComoboService;
	@Autowired
	BaseUnitService baseUnitService;
	@Autowired
	TUnitFzService tUnitFzService;
	@Autowired
	private ClinicItemDictService clinicItemDictService;
	@Autowired
	private TResultGroupService tResultGroupService;
	@Autowired
	private TResultOfficeService tResultOfficeService;
	@Autowired
	private DiseaseManagementService diseaseManagementService;


	/**
	 * 体检首页加载
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"physicalIndex", ""})
	public String clinicMain(PatMasterIndex patMasterIndex, HttpServletRequest request,HttpServletResponse response, Model model) {
		OrgDept orgDept = UserUtils.getUser().getOrgDept();
		model.addAttribute("orgDept",orgDept);
		return "modules/physical/physicalIndex";
	}

	/**
	 * 体检顾客添加页面
	 * @param model
	 * @param patMasterIndex
	 * @param request
	 * @param response
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "addphyUser")
	public String addphyUser(Model model,PatMasterIndex patMasterIndex,HttpServletRequest request,HttpServletResponse response){
		Page<PatMasterIndex> page=patMasterIndexService.findPage(new Page<PatMasterIndex>(request,response),patMasterIndex);
		model.addAttribute("page",page);
		return "modules/physical/userInfo/addUserIndex";
	}

	/**
	 * 体检顾客信息保存
	 * @param patMasterIndex
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "savePhyUserInfo")
	@ResponseBody
	public StringData savePhyUserInfo(PatMasterIndex patMasterIndex){
		StringData data = new StringData();
		int i = physicalService.savePhyUserInfo(patMasterIndex);
		if(i>0){
			data.setCode("success");
			data.setData("保存病人信息成功！");
		}else{
			data.setCode("error");
			data.setData("保存病人信息失败！");
		}
		return data;
	}

	/**
	 * 体检项目录入首页
	 * @param outpTreatRec
	 * @param model
	 * @param request

	 * @param response
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "itemInput")
	public String itemInput (OutpTreatRec outpTreatRec,String deptId,String deptName,Model model,HttpServletRequest request,HttpServletResponse response){
		ExamType examType= new ExamType();
		examType.setLevel("1");
		List<ExamType> examTypeList=this.examTypeService.findList(examType);
		model.addAttribute("examTypeList",examTypeList);

		LabType labType= new LabType();
		labType.setLevel("1");
		List<LabType> labTypeList=this.labTypeService.findList(labType);
		model.addAttribute("labTypeList", labTypeList);

		TItemComobo tItemComobo = new TItemComobo();
		tItemComobo.setDelFlag("0");
		List<TItemComobo> tItemComoboList = tItemComoboService.findList(tItemComobo);
		model.addAttribute("tItemComoboList", tItemComoboList);

//
//		outpTreatRec.setIsPhy("1");
		Page<OutpTreatRec> page = outpTreatRecService.TJfindList(new Page<OutpTreatRec>(request, response), outpTreatRec);
		model.addAttribute("page",page);
		model.addAttribute("outpTreatRec",outpTreatRec);
		return "modules/physical/itemInput/itemInput";
	}

	/**
	 * 项目录入保存
	 * @param outpTreatRec
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "savePhyItem")
	@ResponseBody
	public StringData savePhyItem(OutpTreatRec outpTreatRec){
		StringData stringData = new StringData();
		int i = physicalService.savePhyItem(outpTreatRec);
		if(i>0){
			stringData.setCode("success");
			stringData.setData("保存项目成功！");
		}else{
			stringData.setCode("error");
			stringData.setData("保存项目失败！");
		}
		return stringData;
	}
	/**
	 * 体检顾客信息显示
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "userBasicInfo")
	public String userBasicInfo(String patientId ,String clinicId,String deptId,String deptName, HttpServletRequest request,HttpServletResponse response, Model model){
		PatMasterIndex patMasterIndex = patMasterIndexService.get(patientId);
		TInformation tInformation=tInformationService.findTInfoByPatId(clinicId);
		List<BaseUnit> baseUnit = baseUnitService.findList(new BaseUnit());
		TUnitFz tUnitFz=new TUnitFz();
		tUnitFz.setUnitId(tInformation.getTeam());
		List<TUnitFz> tUnitFz1=tUnitFzService.findList(tUnitFz);
		model.addAttribute("baseUnit", baseUnit);
		model.addAttribute("tUnitFz", tUnitFz1);
		model.addAttribute("basicInfo",patMasterIndex);
		model.addAttribute("clinicId",clinicId);
		model.addAttribute("patientId",patientId);
		model.addAttribute("tInformation",tInformation);
		return "modules/physical/userBasicInfo";
	}

	/**
	 * 获取体检病人列表
	 * @param patMasterIndex
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "queryPatientTj")
	@ResponseBody
	public Page<PatMasterIndex> queryPatientTj(PatMasterIndex patMasterIndex ,Model model,HttpServletRequest request,HttpServletResponse response){
		Page<PatMasterIndex>page=new Page<PatMasterIndex>(request, response);
//		patMasterIndex.setIsPhy("1");
		patMasterIndex.setRegistType("3");
		Page<PatMasterIndex> patMasterIndexPage = patMasterIndexService.queryPatientTj(page,patMasterIndex);
//		model.addAttribute("list",list);
		return patMasterIndexPage;
	}
	/**
	 * 体检顾客信息保存
	 * @param patMasterIndex
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "saveUserBasicInfo")
	@ResponseBody
	public StringData saveUserBasicInfo(PatMasterIndex patMasterIndex){
		StringData data = new StringData();
		int i = patMasterIndexService.saveUserBasicInfo(patMasterIndex);
		if(i>0){
			data.setCode("success");
			data.setData("修改病人信息成功！");
		}else{
			data.setCode("error");
			data.setData("修改病人信息失败！");
		}
		return data;
	}
	/**
	 * 上传头像div
	 * @param
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "image")
	public String image(){
		return "modules/physical/image";
	}


	@RequiresPermissions("user")
	@RequestMapping(value = "sorting")
	public String sorting(String patientId,String clinicId,String deptId,String deptName,TResultGroup tResultGroup,Model model,HttpServletRequest request,HttpServletResponse response){
		tResultGroup.setClinicId(clinicId);
		List<TResultGroupList> lists= tResultGroupService.itemList(clinicId,deptId,UserUtils.getUser().getOrgId());
		TResultOffice tResultOffice =new TResultOffice();
		tResultOffice.setDeptId(deptId);
		tResultOffice.setClinicId(clinicId);
		TResultOffice tResultOffice1 = tResultOfficeService.get(tResultOffice);
		model.addAttribute("lists",lists);
		model.addAttribute("patientId",patientId);
		model.addAttribute("clinicId",clinicId);
		model.addAttribute("deptId",deptId);
		model.addAttribute("deptName",deptName);
		model.addAttribute("tResultOffice",tResultOffice1);
		return "modules/physical/sorting/sortingIndex";
	}



}